package net.sf.rose.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

public class DateUtil {

	/**
	 * date methods block
	 */
	public static String DATE_TYPE_NORMAL = "yyyy-MM-dd";

	public static String DATE_TYPE_STRING = "yyyyMMdd";

	public static String DATE_TYPE_LONG1 = "yyyy-MM-dd HH:mm:ss";

	public static String DATE_TYPE_LONG2 = "yyyyMMddHHmmss";

	public static String DATE_TYPE_LONG3 = "MM-dd-yyyy HH:mm";

	public static String DATE_TYPE_ALL = "yyyy-MM-dd HH:mm:ss a";

	private static String[] dataPatterns = null;

	static {
		dataPatterns = new String[6];
		dataPatterns[0] = DATE_TYPE_NORMAL;
		dataPatterns[1] = DATE_TYPE_STRING;
		dataPatterns[2] = DATE_TYPE_LONG1;
		dataPatterns[3] = DATE_TYPE_LONG2;
		dataPatterns[4] = DATE_TYPE_LONG2;
		dataPatterns[5] = DATE_TYPE_ALL;
	}

	/**
	 * @param args
	 */
	public static void addDataFormatType(String pattern) {
		String[] arr = new String[dataPatterns.length + 1];
		for (int i = 0; i < dataPatterns.length; i++) {
			arr[i] = dataPatterns[i];
		}
		arr[arr.length - 1] = pattern;
		dataPatterns = arr;
	}

	/**
	 * @param args
	 */
	public static java.sql.Date getSqlDate() {
		return new java.sql.Date(new Date().getTime());
	}

	/**
	 * @param args
	 */
	public static Timestamp getTimestamp() {
		return new Timestamp(new Date().getTime());
	}

	/**
	 * @param args
	 */
	public static Date toDate(String arg) {
		try {
			return DateUtils.parseDate(arg, dataPatterns);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @param args
	 * @throws ObjectParseException
	 */
	public static Date toDate(String arg, String pattern) {
		SimpleDateFormat dataFormat = new SimpleDateFormat();
		dataFormat.applyPattern(pattern);
		try {
			return dataFormat.parse(arg);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @param args
	 */
	public static Timestamp toTimestamp(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * @param args
	 * @throws ClassCastException
	 */
	public static Timestamp toTimestamp(String arg) {
		return new Timestamp(toDate(arg).getTime());
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static Timestamp toTimestamp(String arg, String pattern) throws Exception {
		return new Timestamp(toDate(arg, pattern).getTime());
	}

	/**
	 * @param args
	 */
	public static String getDateString() {
		return toString(new Date());
	}

	/**
	 * @param args
	 */
	public static String toString(Date date) {
		return toString(date, DATE_TYPE_NORMAL);
	}

	/**
	 * @param args
	 */
	public static String toString(Date date, String pattern) {
		SimpleDateFormat dataFormat = new SimpleDateFormat();
		dataFormat.applyPattern(pattern);
		return dataFormat.format(date);
	}

	/**
	 * 
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 
	 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 
	 */
	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 
	 */
	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 
	 */
	public static int getSecond(java.util.Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.SECOND);
	}

	/**
	 * 
	 */
	public static double getMinute(Date startDate, Date endDate) {
		return ((endDate.getTime() - startDate.getTime()) / (double) (1000 * 60));
	}

	/**
	 * 
	 */
	public static double getHour(Date startDate, Date endDate) {
		return ((endDate.getTime() - startDate.getTime()) / (double) (1000 * 60 * 60));
	}

	/**
	 * 
	 */
	public static double getDay(Date startDate, Date endDate) {
		return ((endDate.getTime() - startDate.getTime()) / (double) (1000 * 60 * 60 * 24));
	}

	/**
	 * 
	 */
	public static double getMonth(Date startDate, Date endDate) {
		return 1;
	}

	/**
	 * date methods block end.
	 * 
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// System.out.println(new java.util.Date().getTime());
		System.out.println(DateUtil.toDate("02-02-2002 12:30"));
		// System.out.println(DataUtil.DateToString(new java.util.Date()));
	}
}
